package wili.attrMapping.core;

import wili.attrMapping._user.b1.AttConst;
import wili.attrMapping._user.b1.AttMapingRule_main;
import wili.attrMapping._user.b1.AttConst.Bugs;
import wili.c.Ca;
import wili.c.Misc9a;

/**
 * 
 * @author weila 2022年11月6日
 */
class JsonUserConf9 {
	/**
	 * 根据给定的新旧属性集，构造规则。
	 * 
	 * @param genRuleTemplate
	 * @param oldAtts
	 * @param newAtts
	 * @param ruleRoot
	 * @param conf
	 * @return
	 */
	static ValueMapingNode genRuleOfOldNewAtts_then_checkInconsistent(boolean genRuleTemplate, Atts oldAtts,
			Atts newAtts, ValueMapingNode ruleRoot, UserConf conf) {
		/*
		 * >>> GenTemplate 找到所有的旧属性序列、把每个旧属性序列，增加或合并到规则树， >>> GenRule
		 * 找到所有的旧属性序列、把每个旧属性序列，增加或合并到规则树，
		 */
		if (!genRuleTemplate)
			Ca.asert(oldAtts.getKind().equals(newAtts.getKind()), "");
		ValueMapingNode lastAttNod = ruleRoot.addOrMergeAtts(oldAtts, true);
		final ValueMapingNode ruleLeaf = lastAttNod.addOrMergeSon(oldAtts.objName);// 取得规则叶
		if (genRuleTemplate) {
			ruleLeaf.visitTimes++;
			return ruleRoot;
		}
		ruleLeaf.oldAtts.add(oldAtts);
		ruleLeaf.newAtts.add(newAtts);
		int inc2 = ++inc;
		/*
		i19,editable: {
		1: {=(@{formula}) && !${view>/$EditMode/viewMode}}
		in accP.fragment.json, 528a7dff-1398-4fb9-b873-d92369e866a3
		2: {=!${view>/$EditMode/viewMode} && ([undefined,null].includes(${DocEntry}) || ${VDocStatus} !== 'C' && ${VDocStatus} !== 'A' && ${VDeliveryStatus} !== 'N' || ${ObjType} !== '18' && ${ObjType} !== '19' && ${ObjType} !== '20' && ${ObjType} !== '21' && ${ObjType} !== '234000032' && ${ObjType} !== '540000006')}
		in accP.fragment.json, 134d203f-76dd-420c-afb5-42d0cad73837
		3: {=(@{formula}) && #DEFALUT_EDITABLE#}
		in accP.fragment.json, 8bb9e955-f22f-4d62-bd9c-f87e24b1afd0
		}
		 */
		if (inc2 == 143 || ruleLeaf.id == 19)
			Ca.pause();
		String newRuleSt = conf.genRuleOf_oldNewAtts(oldAtts, newAtts);
		String newFileName = newAtts.file.file().getName();
		if (Att9.DEBUG)
			Ca.log("ruleID=" + ruleLeaf.id);
		{//若生成公式同已有公式不同，则视为冲突
			if (ruleLeaf.value == null || ruleLeaf.value.isEmpty()) {
				// 没有规则时，新规则就是规则
				//String va = B1com.andExpToFormular(newRuleSt, ruleLeaf.value);
				ruleLeaf.setValue(newRuleSt);
				ruleLeaf.addValue(newRuleSt, newAtts.key, newFileName);
			} else {
				if (isEquals(ruleLeaf.value, newRuleSt)) {
					Ca.pause();
				} else {
					// 有规则时，若新旧规则不同，视为冲突
					//等价属性集aa共享1个规则叶f，aa[i]会和首值检测冲突。 10次冲突并不意味着有10种值，也许除首值外，其它值都相同。
					Att9.usage("规则=(旧属性，新属性)");
					AttMapingRule_main.bugNum++;
					if (++errInc == -114)
						Ca.asert(ruleLeaf.value.contains("formula"), "todo");
					Ca.log("\n" + errInc + ",i" + ruleLeaf.id + "," + Bugs.msg(Bugs.InconsistentMapping)
							+ oldAtts.file.file().getName() + " ~>" + newFileName);
					int size = ruleLeaf.oldAtts.size();
					String st2 = Misc9a.strOfMisc(ruleLeaf.oldAtts, -2, " ~>", ruleLeaf.newAtts, -2, //
							", \n\trule value:" + ruleLeaf.value);
					String st1 = Misc9a.strOfMisc(ruleLeaf.oldAtts, -1, " ~>", ruleLeaf.newAtts, -1, //
							", \n\trule value:" + newAtts.updateValue);
					String id2 = ruleLeaf.oldAtts.get(size - 2).key;
					String id1 = ruleLeaf.oldAtts.get(size - 1).key;// //findAttFromAncestor("guid", null).stringValue;
					Ca.log(" " + st2 + "\n " + st1);
					String st = " (`" + id2 + "`|`" + id1 + "`)";
					Ca.log(st.replace('`', '"'));
					ruleLeaf.addValue(newRuleSt, newAtts.key, newFileName);
				}
			}
		}
		{// 某fromId 未找到对应的toID, 但当前规则可能已经有值，仅pop此id.
		 // lastAttNod.oldAtts.pop();//
		}
		AttMapingRule_main.scanNum++;
		newAtts.updateValue(ruleLeaf.value);
		ruleRoot.print(-1);
		return lastAttNod;
	}

	static boolean isEquals(String aa, String bb) {
		return aa.replaceAll("\\s+", "").equals(bb.replaceAll("\\s+", ""));
	}

	static int inc, errInc;
}
